using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;
using DIS_DAO.DAO;
using DIS_DAO.Helpers;

namespace DIS_DAO.DTO
{
	/// <summary>
	/// Data transfer object which is associated with Orders table in database
	/// </summary>
	public class OrdersDTO
	{
		public int ID { get; set; }
		public int IDChemistWareHouse { get; set; }
		public int IDCustomer { get; set; }
		public DateTime OrderDate { get; set; }
		public bool IsVerified { get; set; }
		public bool IsShipped { get; set; }
		public bool IsPaid { get; set; }
        public string AddressOfReceiver { get; set; }
        public string NameOfReceiver { get; set; }
        public string PhoneOfReceiver { get; set; }
        public ChemistWareHouseDTO ChemistWareHouse { get; set; }
        public CustomerDTO Customer { get; set; }
        public List<OrderDetailDTO> OrderDetails { get; set; }

		public static OrdersDTO Translate(DataRow row)
		{
			var dto = new OrdersDTO
			{
				ID = int.Parse(row["ID"].ToString()),
				IDChemistWareHouse = int.Parse(row["IDChemistWareHouse"].ToString()),
				OrderDate = DateTime.Parse(row["OrderDate"].ToString()),
				IsVerified = bool.Parse(row["IsVerified"].ToString()),
				IsShipped = bool.Parse(row["IsShipped"].ToString()),
				IsPaid = bool.Parse(row["IsPaid"].ToString()),
                AddressOfReceiver = row["AddressOfReceiver"].ToString(),
                NameOfReceiver = row["NameOfReceiver"].ToString(),
                PhoneOfReceiver = row["PhoneOfReceiver"].ToString(),
                OrderDetails = new List<OrderDetailDTO>()
			};

            if (!string.Empty.Equals(row["IDCustomer"].ToString()))
            {
                dto.IDCustomer = int.Parse(row["IDCustomer"].ToString());
            }

            var rpt = new ChemistWareHouseDAO(GlobalVariables.DbConnectionString);
            var objDS = rpt.SelectByID(dto.IDChemistWareHouse);
            if (objDS != null)
            {
                if (objDS.Tables[0].Rows.Count > 0)
                {
                    dto.ChemistWareHouse = ChemistWareHouseDTO.Translate(objDS.Tables[0].Rows[0]);
                }
            }

            var rpt2 = new CustomerDAO(GlobalVariables.DbConnectionString);
            var objDS2 = rpt2.SelectByID(dto.IDCustomer);
            if (objDS2 != null)
            {
                if (objDS2.Tables[0].Rows.Count > 0)
                {
                    dto.Customer = CustomerDTO.Translate(objDS2.Tables[0].Rows[0]);
                }
            }

            var rpt3 = new OrderDetailDAO(GlobalVariables.DbConnectionString);
            var objDS3 = rpt3.SelectByIDOrder(dto.ID);
            if (objDS3 != null)
            {
                foreach (DataRow item in objDS3.Tables[0].Rows)
                {
                    dto.OrderDetails.Add(OrderDetailDTO.Translate(item));
                }
            }
			return dto;
		}
	}
}